package Queue;

import LinkedList.LinkedList;
import Stack.LinkedListStack;

public class TailLinkedListQueue<E> implements Queue<E>{

    private Node head, tail;

    private int size;

    public TailLinkedListQueue(){
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Queue: size:"+size+" front");
        Node cur = head;
        while(cur != null){
            sb.append(cur + "->");
            cur = cur.next;
        }
        sb.append("NULL  TAIL");
        return sb.toString();
    }

    private class Node{

        private E e;

        private Node next;

        public Node(){
            this(null,null);
        }

        public Node(E e){
            this(null,e);
        }

        public Node(Node next, E e){
            this.next = next;
            this.e = e;
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }


    @Override
    public void enqueue(E e) {
        if (tail == null) {
            tail = new Node(e);
            head = tail;
        }else{
            tail.next = new Node(e);
            tail = tail.next;
        }
        size++;
    }

    @Override
    public E dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("can not dequeue size is 0");
        }
        Node retNode = head;
        head = head.next;
        retNode.next = null;
        if (head == null) {
            tail = null;
        }
        size--;
        return retNode.e;
    }

    @Override
    public E getFront() {
        if (isEmpty()) {
            throw new RuntimeException("can not dequeue size is 0");
        }
        return head.e;
    }



    @Override
    public int getCapacity() {
        return 0;
    }

    public static void main(String[] args) {
        TailLinkedListQueue<Integer> queue = new TailLinkedListQueue<Integer>();
        for (int i = 0; i < 5; i++) {
            queue.enqueue(i);
            System.out.println(queue);
        }
        queue.dequeue();
        System.out.println(queue);
    }
}
